//
// Created by 32827 on 2023/11/25.
//
#include "yzf_dma.h"
YZF_DMA_CHANNEL_POINTER CHANNEL_TEMP=DMA_CH0;
YZF_DMA_POINTER DMA_TEMP=DMA0;
//传输错误标志
YZF_BOOL yzf_dma_flag_TE(YZF_DMA_POINTER dma,uint8_t channel){
    CHANNEL_TEMP=channel;
    DMA_TEMP=dma;
    return dma_flag_get(dma,channel,DMA_FLAG_TAE)==RESET?YZF_FALSE : YZF_TRUE;
}
//半传输标志
YZF_BOOL yzf_dma_flag_HT(YZF_DMA_POINTER dma,uint8_t channel){
    CHANNEL_TEMP=channel;
    DMA_TEMP=dma;
    return dma_flag_get(dma,channel,DMA_FLAG_HTF)==RESET?YZF_FALSE : YZF_TRUE;
}
//传输完成标志
YZF_BOOL yzf_dma_flag_TC(YZF_DMA_POINTER dma,uint8_t channel){
    CHANNEL_TEMP=channel;
    DMA_TEMP=dma;
    return dma_flag_get(dma,channel,DMA_FLAG_FTF)==RESET?YZF_FALSE : YZF_TRUE;
}
//全局中断标志
YZF_BOOL yzf_dma_flag_G(YZF_DMA_POINTER dma,uint8_t channel){
    CHANNEL_TEMP=channel;
    DMA_TEMP=dma;
    return dma_flag_get(dma,channel,DMA_FLAG_SDE)==RESET?YZF_FALSE : YZF_TRUE;
}
//传输错误标志清除
YZF_RESULT yzf_dma_clear_flag_TEIF(YZF_DMA_POINTER dma,uint8_t channel){
    CHANNEL_TEMP=channel;
    DMA_TEMP=dma;
    dma_flag_clear(dma,channel,DMA_FLAG_TAE);
    return YZF_OK;
}
//半传输标志清除
YZF_RESULT yzf_dma_clear_flag_HTIF(YZF_DMA_POINTER dma,uint8_t channel){
    CHANNEL_TEMP=channel;
    DMA_TEMP=dma;
    dma_flag_clear(dma,channel,DMA_FLAG_HTF);
    return YZF_OK;
}
//传输完成标志清除
YZF_RESULT yzf_dma_clear_flag_TCIF(YZF_DMA_POINTER dma,uint8_t channel){
    CHANNEL_TEMP=channel;
    DMA_TEMP=dma;
    dma_flag_clear(dma,channel,DMA_FLAG_FTF);
    return YZF_OK;
}
//全局中断标志清除
YZF_RESULT yzf_dma_clear_flag_GIF(YZF_DMA_POINTER dma,uint8_t channel){
    CHANNEL_TEMP=channel;
    DMA_TEMP=dma;
    dma_flag_clear(dma,channel,DMA_FLAG_SDE);
    return YZF_OK;
}

//存储器到存储器
YZF_RESULT yzf_dma_mode_mem_mem(YZF_DMA_CHANNEL_POINTER dma){
    CHANNEL_TEMP=dma;
    dma_transfer_direction_config(DMA_TEMP, dma, DMA_MEMORY_TO_MEMORY);
    return YZF_OK;
}
//非存储器到存储器
YZF_RESULT yzf_dma_mode_per_mem(YZF_DMA_CHANNEL_POINTER dma){
    CHANNEL_TEMP=dma;
    dma_transfer_direction_config(DMA_TEMP, dma, DMA_PERIPH_TO_MEMORY);
    return YZF_OK;
}

//设置通道优先级
YZF_RESULT yzf_dma_set_priority(YZF_DMA_CHANNEL_POINTER dma,YZF_DMA_PRIORITY priority){
    CHANNEL_TEMP=dma;
    dma_priority_config(DMA_TEMP, dma, priority);
    return YZF_OK;
}

//存储器数据宽度
YZF_RESULT yzf_dma_mem_size(YZF_DMA_CHANNEL_POINTER dma,YZF_DMA_SIZE size){
    CHANNEL_TEMP=dma;
    dma_memory_width_config(DMA_TEMP, dma,  size);
    return YZF_OK;
}
//设备数据宽度
YZF_RESULT yzf_dma_per_size(YZF_DMA_CHANNEL_POINTER dma,YZF_DMA_SIZE size){
    CHANNEL_TEMP=dma;
    dma_periph_width_config(DMA_TEMP, dma,  size);
    return YZF_OK;
}

//内存是否递增
YZF_RESULT yzf_dma_mode_mem_inc(YZF_DMA_CHANNEL_POINTER dma,YZF_BOOL enable){
    CHANNEL_TEMP=dma;
    if(enable){
        dma_memory_address_generation_config(DMA_TEMP, dma,DMA_MEMORY_INCREASE_ENABLE);
    }else{
        dma_memory_address_generation_config(DMA_TEMP, dma,DMA_MEMORY_INCREASE_DISABLE);
    }
    return YZF_OK;
}
//设备是否递增
YZF_RESULT yzf_dma_mode_per_inc(YZF_DMA_CHANNEL_POINTER dma,YZF_BOOL enable){
    CHANNEL_TEMP=dma;
    if(enable){
        dma_peripheral_address_generation_config(DMA_TEMP, dma, DMA_PERIPH_INCREASE_ENABLE);
    }else{
        dma_peripheral_address_generation_config(DMA_TEMP, dma, DMA_PERIPH_INCREASE_DISABLE);
    }
    return YZF_OK;
}

//循环接收
YZF_RESULT yzf_dma_mode_circular(YZF_DMA_CHANNEL_POINTER dma,YZF_BOOL enable){
    CHANNEL_TEMP=dma;
    if(enable){
        dma_circulation_enable(DMA_TEMP, dma);
    }else{
        dma_circulation_disable(DMA_TEMP, dma);
    }
    return YZF_OK;
}
uint8_t memory_flag=DMA_MEMORY_0;
//从外设读
YZF_RESULT yzf_dma_mode_per_read(YZF_DMA_CHANNEL_POINTER dma){
    CHANNEL_TEMP=dma;
    memory_flag=DMA_MEMORY_1;
    return YZF_OK;
}
//从内存读
YZF_RESULT yzf_dma_mode_mem_read(YZF_DMA_CHANNEL_POINTER dma){
    CHANNEL_TEMP=dma;
    memory_flag=DMA_MEMORY_0;
    return YZF_OK;
}

//传输错误中断
YZF_RESULT yzf_dma_interrupt_TE(YZF_DMA_CHANNEL_POINTER dma,YZF_BOOL enable){
    CHANNEL_TEMP=dma;
    if(enable){
        dma_interrupt_enable(DMA_TEMP, dma, DMA_CHXCTL_TAEIE);
    }else{
        dma_interrupt_disable(DMA_TEMP, dma, DMA_CHXCTL_TAEIE);
    }
    return YZF_OK;
}
//传输完成中断
YZF_RESULT yzf_dma_interrupt_TC(YZF_DMA_CHANNEL_POINTER dma,YZF_BOOL enable){
    CHANNEL_TEMP=dma;
    if(enable){
        dma_interrupt_enable(DMA_TEMP, dma, DMA_CHXCTL_FTFIE);
    }else{
        dma_interrupt_disable(DMA_TEMP, dma, DMA_CHXCTL_FTFIE);
    }
    return YZF_OK;
}
//半传输中断
YZF_RESULT yzf_dma_interrupt_HT(YZF_DMA_CHANNEL_POINTER dma,YZF_BOOL enable){
    CHANNEL_TEMP=dma;
    if(enable){
        dma_interrupt_enable(DMA_TEMP, dma, DMA_CHXCTL_HTFIE);
    }else{
        dma_interrupt_disable(DMA_TEMP, dma, DMA_CHXCTL_HTFIE);
    }
    return YZF_OK;
}

YZF_RESULT yzf_dma_enable(YZF_DMA_CHANNEL_POINTER dma){
    CHANNEL_TEMP=dma;
    dma_channel_enable(DMA_TEMP, dma);
    return YZF_OK;
}
YZF_RESULT yzf_dma_disable(YZF_DMA_CHANNEL_POINTER dma){
    CHANNEL_TEMP=dma;
    dma_channel_enable(DMA_TEMP, dma);
    return YZF_OK;
}
YZF_RESULT yzf_dma_transmit_count(YZF_DMA_CHANNEL_POINTER dma,uint16_t count){
    CHANNEL_TEMP=dma;
    dma_transfer_number_config(DMA_TEMP, dma, count);
    return YZF_OK;
}
//设备地址
YZF_RESULT yzf_dma_per_address(YZF_DMA_CHANNEL_POINTER dma,uint32_t address){
    CHANNEL_TEMP=dma;
    dma_periph_address_config(DMA_TEMP, dma, address);
    return YZF_OK;
}
//内存地址
YZF_RESULT yzf_dma_mem_address(YZF_DMA_CHANNEL_POINTER dma,uint32_t address){
    CHANNEL_TEMP=dma;
    dma_memory_address_config(DMA_TEMP, dma,memory_flag,address);
    return YZF_OK;
}
YZF_RESULT yzf_dma_clock_enable(YZF_DMA_POINTER dma){
    DMA_TEMP=dma;
    switch ((uint32_t)dma) {
        case (uint32_t)DMA0:
            rcu_periph_clock_enable(RCU_DMA0);
            break;
        case (uint32_t)DMA1:
            rcu_periph_clock_enable(RCU_DMA1);
            break;
        default:
            return YZF_NOT_FOUND;
    }
    return YZF_OK;
}
YZF_RESULT yzf_dma_clock_disable(YZF_DMA_POINTER dma){
    DMA_TEMP=dma;
    switch ((uint32_t)dma) {
        case (uint32_t)DMA0:
            rcu_periph_clock_disable(RCU_DMA0);
            break;
        case (uint32_t)DMA1:
            rcu_periph_clock_disable(RCU_DMA1);
            break;
        default:
            return YZF_NOT_FOUND;
    }
    return YZF_OK;
}